using System.Collections;
using System.Text.RegularExpressions;

namespace Communication
{
    /// <summary>
    /// Contains information about the supported new message indications of the phone.
    /// </summary>
    public class MessageIndicationSupport
    {
        private readonly string _buffer;
        private readonly string _cellBroadcast;
        private readonly string _deliver;
        private readonly string _mode;
        private readonly string _statusReport;

        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="mode">A string representation of the phone's supported indication modes</param>
        /// <param name="deliver">A string representation of the phones's supported standard SMS (SMS-DELIVER) styles</param>
        /// <param name="cellBroadcast">A string representation of the phones's supported cell broadcast styles</param>
        /// <param name="statusReport">The phones's supported status report (SMS-STATUS-REPORT) styles</param>
        /// <param name="buffer">The phones's supported buffer handling settings</param>
        public MessageIndicationSupport(string mode, string deliver, string cellBroadcast, string statusReport, string buffer)
        {
            _mode = mode;
            _deliver = deliver;
            _cellBroadcast = cellBroadcast;
            _statusReport = statusReport;
            _buffer = buffer;
        }

        private static ArrayList ParseArrayAsString(string s)
        {
            var list = new ArrayList();
            var match = new Regex(@"(?:(\d+),?)+").Match(s);
            if (match.Success)
                foreach (Capture capture in match.Groups[1].Captures)
                    list.Add(int.Parse(capture.Value));
            var match2 = new Regex(@"(\d+)-(\d+)").Match(s);
            if (match2.Success)
            {
                var num = int.Parse(match2.Groups[1].Value);
                var num2 = int.Parse(match2.Groups[2].Value);
                for (var i = num; i <= num2; i++)
                    list.Add(i);
            }
            return list;
        }

        /// <summary>
        /// Checks if a specific buffer handling setting is supported.
        /// </summary>
        /// <param name="setting">The setting to check</param>
        /// <returns>true if the setting is supported, false otherwise.</returns>
        public bool SupportsBufferSetting(IndicationBufferSetting setting)
        {
            return SupportsBufferSetting((int) setting);
        }

        /// <summary>
        /// Checks if a specific buffer handling setting is supported.
        /// </summary>
        /// <param name="setting">The setting to check</param>
        /// <returns>true if the setting is supported, false otherwise.</returns>
        public bool SupportsBufferSetting(int setting)
        {
            return ParseArrayAsString(_buffer).Contains(setting);
        }

        /// <summary>
        /// Checks if a specific cell broadcast indication style is supported.
        /// </summary>
        /// <param name="style">The style to check</param>
        /// <returns>true if the style is supported, false otherwise.</returns>
        public bool SupportsCellBroadcastStyle(CbmIndicationStyle style)
        {
            return SupportsCellBroadcastStyle((int) style);
        }

        /// <summary>
        /// Checks if a specific cell broadcast indication style is supported.
        /// </summary>
        /// <param name="style">The style to check</param>
        /// <returns>true if the style is supported, false otherwise.</returns>
        public bool SupportsCellBroadcastStyle(int style)
        {
            return ParseArrayAsString(_cellBroadcast).Contains(style);
        }

        /// <summary>
        /// Checks if a specific SMS-DELIVER indication style is supported.
        /// </summary>
        /// <param name="style">The style to check</param>
        /// <returns>true if the style is supported, false otherwise.</returns>
        public bool SupportsDeliverStyle(SmsDeliverIndicationStyle style)
        {
            return SupportsDeliverStyle((int) style);
        }

        /// <summary>
        /// Checks if a specific SMS-DELIVER indication style is supported.
        /// </summary>
        /// <param name="style">The style to check</param>
        /// <returns>true if the style is supported, false otherwise.</returns>
        public bool SupportsDeliverStyle(int style)
        {
            return ParseArrayAsString(_deliver).Contains(style);
        }

        /// <summary>
        /// Checks if a specific indication mode is supported.
        /// </summary>
        /// <param name="mode">The mode to check</param>
        /// <returns>true if the mode is supported, false otherwise.</returns>
        public bool SupportsMode(MessageIndicationMode mode)
        {
            return SupportsMode((int) mode);
        }

        /// <summary>
        /// Checks if a specific indication mode is supported.
        /// </summary>
        /// <param name="mode">The mode to check</param>
        /// <returns>true if the mode is supported, false otherwise.</returns>
        public bool SupportsMode(int mode)
        {
            return ParseArrayAsString(_mode).Contains(mode);
        }

        /// <summary>
        /// Checks if a specific status report (SMS-STATUS-REPORT) indication style is supported.
        /// </summary>
        /// <param name="style">The style to check</param>
        /// <returns>true if the style is supported, false otherwise.</returns>
        public bool SupportsStatusReportStyle(SmsStatusReportIndicationStyle style)
        {
            return SupportsStatusReportStyle((int) style);
        }

        /// <summary>
        /// Checks if a specific status report (SMS-STATUS-REPORT) indication style is supported.
        /// </summary>
        /// <param name="style">The style to check</param>
        /// <returns>true if the style is supported, false otherwise.</returns>
        public bool SupportsStatusReportStyle(int style)
        {
            return ParseArrayAsString(_statusReport).Contains(style);
        }

        /// <summary>
        /// Gets a string representation of the supported buffer handling settings.
        /// </summary>
        public string BufferHandling
        {
            get
            {
                return _buffer;
            }
        }

        /// <summary>
        /// Gets a string representation of the supported cell broadcast indication styles.
        /// </summary>
        public string CellBroadcastStyles
        {
            get
            {
                return _cellBroadcast;
            }
        }

        /// <summary>
        /// Gets a string representation of the supported deliver indication modes.
        /// </summary>
        public string DeliverStyles
        {
            get
            {
                return _deliver;
            }
        }

        /// <summary>
        /// Gets a string representation of the supported indication modes.
        /// </summary>
        public string Modes
        {
            get
            {
                return _mode;
            }
        }

        /// <summary>
        /// Gets a string representation of the supported status report indication styles.
        /// </summary>
        public string StatusReportStyles
        {
            get
            {
                return _statusReport;
            }
        }
    }
}